<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
    //add question
    public function add()
    {
        if (!user_ins()->is_logged_in())
            return ['status => 0', 'msg' => 'no login'];

        if (!rq('title'))
            return ['status => 0', 'msg' => 'no title'];
        $this->title = rq('title');
        $this->user_id = session('user_id');

        if (rq('desc'))
            $this->desc = rq('desc');

        return $this->save() ?
            ['status' => 200, 'id' => $this->id] :
            ['status' => 0, 'msg' => 'insert failed'];

    }

    //update question
    public function change()
    {
        if (!user_ins()->is_logged_in())
            return ['status => 0', 'msg' => 'no login'];

        if (!rq('id'))
            return ['status => 0', 'msg' => 'no id'];

        $question = $this->find(rq('id'));
        if (empty($question))
            return ['status => 0', 'msg' => 'no fund question'];

        if ($question->user_id != session('user_id'))
            return ['status => 0', 'msg' => 'no auth'];

        if (rq('title'))
            $question->title = rq('title');
        if (rq('desc'))
            $question->desc = rq('desc');

        return $question->save() ?
            ['status' => 200] :
            ['status' => 0, 'msg' => 'update failed'];
    }

    //
    public function read()
    {
        if (rq('id')) {
            $r = $this->with('answers_with_user')
                ->find(rq('id'));
            return ['status' => 1, 'data' => $r];
        }
        if (rq('user_id')) {
            $user_id = rq('user_id') == 'self' ? session('user_id') : rq('user_id');

            if (rq('user_id') == 'self' && !is_logged_in())
                return err('login required');

            return $this->read_by_user_id($user_id);
        }
        //limit
        //$limit = rq('limit') ?: 15;
        //offset
        //$skip = (rq('page') > 0 ? rq('page') - 1 : 0) * $limit;
        list($limit, $skip) = paginate(rq('page'), rq('limit'));

        //构建query并返回collection数组
        $res = $this->orderBy('created_at')
            ->limit($limit)
            ->skip($skip)
            ->get(['id', 'title', 'desc', 'user_id', 'created_at'])
            ->keyBy('id');


        return ['status' => 1, 'data' => $res];
    }

    public function read_by_user_id($user_id)
    {
        return ['status' => 1, 'data' => $this->where('user_id', $user_id)->get()->toArray()];
    }

    //del question
    public function remove()
    {
        if (!user_ins()->is_logged_in())
            return ['status => 0', 'msg' => 'no login'];

        if (!rq('id'))
            return ['status => 0', 'msg' => 'no id'];

        $question = $this->find(rq('id'));
        if (!$question) return ['status' => 0, 'msg' => 'no question'];
        if ($question->user_id != session('user_id'))
            return ['status => 0', 'msg' => 'no auth'];

        return $question->delete() ?
            ['status' => 200] :
            ['status' => 0, 'msg' => 'delete failed'];
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function answers()
    {
        return $this->hasMany('App\Answer');
    }

    public function answers_with_user()
    {
        return $this->answers()->with('user','users');
    }
}
